using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;

namespace DAL
{
	#region Generated code
    public class BlueBillDataAccess : FreshCmsAccessBase
    {
        public List<BlueBill> GetBlueBillList()
        {
            try
            {
                const string spName = "sp_BlueBill_GetList";

                var blueBillList = DbHelper.ExecuteSPList<BlueBill>(spName);
                return blueBillList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return new List<BlueBill>();
            }
        }

        public List<BlueBill> GetBlueBillListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "sp_BlueBill_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var blueBillList = DbHelper.ExecuteSqlList<BlueBill>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return blueBillList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return new List<BlueBill>();
            }
        }

        public BlueBill GetBlueBillById(int billId)
        {
            try
            {
                const string spName = "sp_BlueBill_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BillId", billId)
                                    };

                var blueBill = DbHelper.ExecuteSPList<BlueBill>(spName, sqlParams).SingleOrDefault();
                return blueBill;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return null;
            }
        }

        public int InsertBlueBill(BlueBill blueBill)
        {
            try
            {
                const string spName = "sp_BlueBill_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@ProId", blueBill.ProId),
										new SqlParameter("@BillQuantity", blueBill.BillQuantity),
										new SqlParameter("@BillTotal", blueBill.BillTotal),
										new SqlParameter("@BillDateSell", blueBill.BillDateSell),
										new SqlParameter("@BillNote", blueBill.BillNote),
										new SqlParameter("@UserId", blueBill.UserId),
										new SqlParameter("@BillId", blueBill.BillId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@BillId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@BillId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@BillId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }

        public int UpdateBlueBill(BlueBill blueBill)
        {
            try
            {
                const string spName = "sp_BlueBill_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BillId", blueBill.BillId)
										, new SqlParameter("@ProId", blueBill.ProId)
										, new SqlParameter("@BillQuantity", blueBill.BillQuantity)
										, new SqlParameter("@BillTotal", blueBill.BillTotal)
										, new SqlParameter("@BillDateSell", blueBill.BillDateSell)
										, new SqlParameter("@BillNote", blueBill.BillNote)
										, new SqlParameter("@UserId", blueBill.UserId)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }

        public int DeleteBlueBill(int billId)
        {
            try
            {
                const string spName = "sp_BlueBill_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BillId", billId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
